package com.icesoft.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.icesoft.framework.core.service.impl.BaseServiceImpl;
import com.icesoft.system.entity.SubOrg;
import com.icesoft.system.mapper.SubOrgMapper;
import com.icesoft.system.service.SubOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * appuserService业务层处理
 *
 * @author madiao
 * @date 230212
 */
@Slf4j
@Service
public class SubOrgServiceImpl extends BaseServiceImpl<SubOrgMapper, SubOrg> implements SubOrgService {

	public List<SubOrg> findAllParentOrg(Long id) {
		return lambdaQuery().eq(SubOrg::getSubOrgId, id).list();
	}

	public List<SubOrg> findAllSubOrg(Long id) {
		return lambdaQuery().eq(SubOrg::getOrgId, id).list();
	}


	public List<Long> findAllParentOrgIds(Long id) {
		List<Long> ids = new ArrayList<>();
		List<SubOrg> allParentOrg = findAllParentOrg(id);
		allParentOrg.forEach(subOrg -> ids.add(subOrg.getOrgId()));
		return ids;
	}

	public List<Long> findAllSubOrgIds(Long id) {
		List<Long> ids = new ArrayList<>();
		List<SubOrg> allParentOrg = findAllSubOrg(id);
		allParentOrg.forEach(subOrg -> ids.add(subOrg.getSubOrgId()));
		return ids;
	}

	public void deleteByOrgId(Serializable orgId) {
		LambdaQueryWrapper<SubOrg> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SubOrg::getOrgId, orgId)
				.or()
				.eq(SubOrg::getSubOrgId, orgId);
		remove(wrapper);
	}
}
